<?php

class Admin_Model_About extends Zend_Db_Table {

    protected $_name = 'about';
    protected $_primary = 'id';

    public function countItem($arrParam = null, $option = null) {
        $db = Zend_Registry::get('connectDb');
        //$db = Zend_Db::factory($adapter, $config);
        $ssFilter = $arrParam['ssFilter'];

        $select = $db->select()
                ->from('about AS a', array('COUNT(a.id) AS totalItem'));
        if (!empty($ssFilter['keywords'])) {
            $keywords = '%' . $ssFilter['keywords'] . '%';
            $select->where('a.content LIKE ? OR a.content_vi LIKE ? ', $keywords, $keywords); // doi so thu 3 ep kieu STRING
        }

        $result = $db->fetchOne($select);
        return $result;
    }

    public function listItem($arrParam = null, $option = null) {
//            echo "<pre>"; print_r($arrParam); die();
        $db = Zend_Registry::get('connectDb');
        //$db = Zend_Db::factory($adapter, $config);
        $paginator = $arrParam['paginator'];
        $ssFilter = $arrParam['ssFilter'];

        if ($option['task'] == 'admin-list') {
            $select = $db->select()
                    ->from('about AS a');

            if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {

                $select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
            }


            if ($paginator['itemCountPerPage'] > 0) {
                $page = $paginator['currentPage'];
                $rowCount = $paginator['itemCountPerPage'];
                $select->limitPage($page, $rowCount);
            }
            if (!empty($ssFilter['keywords'])) {
                $keywords = '%' . $ssFilter['keywords'] . '%';
                $select->where('a.content LIKE ? OR a.content_vi LIKE ? ', $keywords, $keywords); // doi so thu 3 ep kieu STRING
            }

            $result = $db->fetchAll($select);
        }
        return $result;
    }

    public function uploadFile($arrInfor) {
        
        // Duong dan den thu muc upload
        $upload_dir             = PUBLIC_IMG_PATH.'/other';

        $crrLargeImg            = $arrInfor['current_image'];
        // upload file
        $upload = new Australian_File_Upload();
        
        $fileInfo = $upload->getFileInfo('image');
        $fileName = $fileInfo['image']['name'];
        
        if (!empty($fileName)) {
            if ($arrInfor['action'] == 'edit') {
                if (file_exists($crrLargeImg)){
                    $upload->removeFile($upload_dir . '/' . $crrLargeImg);
                }
                
            }
            
            $upload->uploadslide('image', $upload_dir, $crrLargeImg);
            $thumb = Australian_File_Images::create($upload_dir .'/'. $crrLargeImg);
            $thumb->resize(170, 95)->save($upload_dir .'/'. $crrLargeImg);
        }
    }

    public function saveItem($arrParam = null, $option = null) {

        if ($option['task'] == 'admin-add') {
            $row                            = $this->fetchNew();
            $row->en_content                = $arrParam['en_content'];
            $row->vi_content                = $arrParam['vi_content'];
            $row->status                    = $arrParam['status'];
            $row->datecreated               = date("Y-m-d H:i:s");
            $row->uidcreated                = $arrParam['userinfor']['username'];
            $id                             = $row->save();
            
            $image                          = "about_".$id.".jpg";
            $arrParam['current_image']      = $image;
            $this->uploadFile($arrParam);
            $where = 'id = ' . $id;
            $rowupdate = $this->fetchRow($where);
            $rowupdate->image               = $image;
            $rowupdate->save();
        }
        if ($option['task'] == 'admin-edit') {
            $image                          = "about_".$arrParam['id'].".jpg";
            $arrParam['current_image']      = $image;
            $this->uploadFile($arrParam);
            $where                          = 'id = ' . $arrParam['id'];
            $row                            = $this->fetchRow($where);
            $row->en_content                = $arrParam['en_content'];
            $row->vi_content                = $arrParam['vi_content'];
            $row->status                    = $arrParam['status'];
            $row->image                     = $image;
            $row->dateupdated               = date("Y-m-d H:i:s");
            $row->uidupdated                = $arrParam['userinfor']['username'];
            $row->save();
        }
    }

    public function getItem($arrParam = null, $option = null) {

        if ($option['task'] == 'admin-info' || $option['task'] == 'admin-edit') {
            $where = 'id = ' . $arrParam['id'];
            $result = $this->fetchRow($where)->toArray();
        }

        return $result;
    }

    public function deleteItem($arrParam = null, $option = null) {
        $upload                 = new Australian_File_Upload();
        $upload_dir             = PUBLIC_IMG_PATH . '/other';
        if ($option['task'] == 'admin-delete') {
            $where = 'id = ' . $arrParam['id'];
            $this->delete($where);
            $largeImg           = $upload_dir . '/' . 'about_'.$arrParam['id'].'.jpg';
            if (file_exists($largeImg)){
                $upload->removeFile($largeImg);
            }
        }

        if ($option['task'] == 'admin-multi-delete') {
            $cid = $arrParam['cid'];
            if (count($cid) > 0) {
                if ($arrParam['type'] == 1) {
                    $status = 1;
                } else {
                    $status = 0;
                }
                $ids = implode(',', $cid);
                $where = 'id IN (' . $ids . ')';
                $this->delete($where);
                foreach ($cid as $value) {
                    $largeImg           = $upload_dir . '/' . 'about_'.$value.'.jpg';
                    if (file_exists($largeImg)){
                        $upload->removeFile($largeImg);
                    }
                }
            }
        }
    }

    public function changeStatua($arrParam = null, $option = null) {

        $cid = $arrParam['cid'];
        if (count($cid) > 0) {
            if ($arrParam['type'] == 1) {
                $status = 1;
            } else {
                $status = 0;
            }
            $ids = implode(',', $cid);
            $data = array('status' => $status);
            $where = 'id IN (' . $ids . ')';
            $this->update($data, $where);
            // UPDATE FROM user_group SET status = 1 WHERE id IN (1,2,3)
        }
        if ($arrParam['id'] > 0) {
            if ($arrParam['type'] == 1) {
                $status = 1;
            } else {
                $status = 0;
            }

            $data = array('status' => $status);
            $where = 'id = ' . $arrParam['id'];
            $this->update($data, $where);
        }
    }

}